define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cutbill/index' + location.search,
                    add_url: 'cutbill/add',
                    edit_url: 'cutbill/edit',
                    del_url: 'cutbill/del',
                    multi_url: 'cutbill/multi',
                    import_url: 'cutbill/import',
                    table: 'cutbill',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'purchase_id', title: __('Purchase_id')},
                        {field: 'bill_id', title: __('Bill_id')},
                        {field: 'bank_id', title: __('Bank_id')},
                        {field: 'sn', title: __('Sn')},
                        {field: 'clothtype_id', title: __('Clothtype_id')},
                        {field: 'color', title: __('Color'), operate: 'LIKE'},
                        {field: 'used_nums', title: __('Used_nums'), operate:'BETWEEN'},
                        {field: 'goods_nums', title: __('Goods_nums')},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'purchase_sn', title: __('Purchase_sn'), operate: 'LIKE'},
                        {field: 'process_fee', title: __('Process_fee'), operate:'BETWEEN'},
                        {field: 'cost_every', title: __('Cost_every'), operate:'BETWEEN'},
                        {field: 'cost_total', title: __('Cost_total'), operate:'BETWEEN'},
                        {field: 'shipment_nums', title: __('Shipment_nums'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        produce: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cutbill/produce' + location.search,
                    add_url: 'cutbill/add',
                    //edit_url: 'cutbill/edit',
                    //del_url: 'cutbill/del',
                    multi_url: 'cutbill/multi',
                    import_url: 'cutbill/import',
                    table: 'cutbill',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('行号')},
                        {field: 'cut.factory_id', title: __('生产厂家'), sortable: true, formatter: Table.api.formatter.status, searchList: Config.Factorys},
                        {field: 'clothtype_id', title: __('Clothtype_id'), sortable: true, formatter: Table.api.formatter.status, searchList: Config.Clothtype},
                        {field: 'bill_id', title: __('进货信息'), renderDefault: false, formatter: function (value, row, index) {
                                return row.purchase_bill.clothbank + '[' + row.purchase_bill.color + ']' + '[' + row.purchase_bill.nums + ']';
                            }
                        },
                        {field: 'purchase_sn', title: __('Purchase_sn')},
                        {field: 'cut.remark', title: __('生产安排')},
                        {field: 'sn', title: __('Sn')},
                        {field: 'total_nums', title: __('Goods_nums')},
                        {field: 'deliver_nums', title: __('Shipment_nums')},
                        {field: 'remain_nums', title: __('未出货数')},
                        {field: 'cost_every', title: __('Cost_every')},
                        {field: 'process_fee', title: __('Process_fee')},
                        {field: 'createtime', title: __('安排时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'status', title: __('Status'), sortable: true, formatter: Table.api.formatter.status, searchList: __('Produce_status')},
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
								{
                                    name: 'cut',
                                    text: '详情',
                                    title: '详情',
                                    classname: 'btn btn-xs btn-primary  btn-dialog',
                                    extend: 'data-area=\'["99%","99%"]\' data-shade=\'[0.5,"#000"]\'',
                                    url: function ($row) {
   									    return 'cutbill/produceInfo?ids='+ $row.id;
   									},
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'cutbill/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'cutbill/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'cutbill/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        produceinfo: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
